// pages/my/my.js
import request from "../../utils/request";

let startY = 0;//手指起始的坐标
let moveY = 0;//手指移动的坐标
let moveDistance = 0;//手指移动的距离
Page({

    /**
     * 页面的初始数据
     */
    data: {
        coverTransform: 'translateY(0)',
        coverTransition: '',
        userInfo: {},//用户信息
        recentPlayList: [],//用户的播放记录
        songList: [],//用户的歌单
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //读取用户的基本信息
        let userInfo = wx.getStorageSync('userInfo');
        if (userInfo) {
            //更新userInfo的状态
            this.setData({
                userInfo: JSON.parse(userInfo)
            })

            //获取用户播放记录
            this.getUserRecentPlayList(this.data.userInfo.userId)

            //获取用户歌单
            this.getUserSongList(this.data.userInfo.userId)
        }
    },

    //获取用户播放记录的方法(函数)
    async getUserRecentPlayList(userId) {
        let recentPlayListData = await request('/user/record', {uid: userId, type: 0})
        let index = 0;
        let recentPlayList = recentPlayListData.allData.splice(0, 10).map(item => {
            item.id = index++;
            return item;
        })
        this.setData({
            recentPlayList
        })
    },

    //获取用户歌单的方法(函数)
    async getUserSongList(userId) {
        let songListData = await request('/user/playlist', {uid: userId})
        let index = 0;
        let songList = songListData.playlist.splice(1, 10)
        this.setData({
            songList
        })
    },

    handleTouchStart(event) {
        this.setData({
            coverTransition: '',
        })
        //获取手指的起始坐标
        startY = event.touches[0].clientY;
    },
    handleTouchMove(event) {
        moveY = event.touches[0].clientY;
        moveDistance = moveY - startY

        if (moveDistance <= 0) {
            return;
        }
        if (moveDistance >= 80) {
            moveDistance = 80;
        }
        //动态更新coverTransform的状态值
        this.setData({
            coverTransform: `translateY(${moveDistance}rpx)`
        })
    },
    handleTouchEnd() {
        this.setData({
            coverTransform: `translateY(0rpx)`,
            coverTransition: 'transform 1s linear',
        })
    },

    // 跳转至登录页面的回调
    toLogin() {
        if (!this.data.userInfo) {//判断是否已登录
            wx.showToast({
                title: '已登录账号',
                icon: 'none'
            })
        }else {
            wx.navigateTo({
                url: '/pages/login/login'
            })
        }
    },

    //跳转至other页面
    toOther() {
        wx.navigateTo({
            url: '../../otherPackage/pages/other/other'
        })
    },

    //跳转到歌单歌曲列表页面
    toSonglist(event) {
        console.log(event)
        wx.navigateTo({
            url: '/songPackage/pages/songlist/songlist?id=' + event.currentTarget.id
        })
    },

    //跳转至songDetail页面
    toSongDetail(event) {
        console.log(event);
        let {song, index} = event.currentTarget.dataset.song;
        this.setData({
            index
        })
        // console.log(song);
        //路由跳转传参: query参数
        wx.navigateTo({
            // 不能直接将song对象作为参数传递，长度过长，会被自动截取掉
            // url: '/pages/songDetail/songDetail?song=' + JSON.stringify(song)
            url: '/songPackage/pages/songDetail/songDetail?musicId=' + song.id
        })
    },

    toGiveMoney() {
        wx.showToast({
            title: "就你，也配？凸( •̀ 3 •́ )凸",
            icon: "none"
        })
    },

    // delateUserMessage() {//注销账户
    //     this.setData({
    //         userInfo: {},
    //     })
    // },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})